<?php
require_once('../lib/converthelper.php');
require_once('../lib/moriarty/httprequest.class.php');

class ConvertHelper_TestCase extends PHPUnit_TestCase{
	
	var $object;
	
	function __construct($name)
	{
		$this->PHPUnit_TestCase($name);
	}
	
	function setUp(){
		$this->object = new ConvertHelper();
	}
	
	function tearDown(){
		unset($this->object);
	}
	
	function test_accept_header_to_list(){
		$header = 'text/html,image/jpeg,image/png;q=0.7,application/rdf+xml';
		$expected_array=array('text/html', 'image/jpeg', 'application/rdf+xml', 'image/png');
		$expected = array_pop($expected_array);
		$actual = array_pop($this->object->accept_header_to_list($header));
		$this->assertEquals($expected, $actual);
	}

	function test_choose_input_with_html_and_mimetype(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'text/html; charset=utf-8');
		$response->status_code=200;
		$response->body = '<html></html>';
		$expected = 'rdf-html';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}
	
	
	function test_choose_input_with_sparql_and_vague_mimetype(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'application/xml');
		$response->status_code=200;
		$response->body = file_get_contents('documents/sparql-sample.srx');
		
		$expected = 'sparql-xml';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}
	function test_choose_input_with_sparql_and_specific_mimetype(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'application/sparql-results+xml');
		$response->status_code=200;
		$response->body = file_get_contents('documents/sparql-sample.srx');		
		$expected = 'sparql-xml';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}
	function test_choose_input_with_facets_xml(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'application/xml');
		$response->status_code=200;
		$response->body = file_get_contents('documents/facet-sample.xml');
		$expected = 'facet-xml';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}

	function test_choose_input_with_rdf_xml(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'application/xml');
		$response->status_code=200;
		$response->body = file_get_contents('documents/rdf-sample.xml');
		$expected = 'rdf-xml';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}

	function test_choose_input_with_rdf_turtle(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'text/plain');
		$response->status_code=200;
		$response->body = file_get_contents('documents/rdf.ttl');
		$expected = 'rdf-turtle';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}

	function test_choose_input_with_rdf_json(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'application/json');
		$response->status_code=200;
		$response->body = file_get_contents('documents/rdf.json');
		$expected = 'rdf-json';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}

	function test_choose_input_with_invalid_rdf_json(){
		
		$url = '';
		$response = new HTTPResponse();
		$response->headers = array('content-type'=> 'application/json');
		$response->status_code=200;
		$response->body = file_get_contents('documents/bad.json');
		$expected = 'invalid-json';
		$actual = $this->object->choose_input($url, $response);
		$this->assertEquals($expected, $actual);
	}



	function test_get_filename_extension_success(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-xml', 'json' );
		$expected = 'json';
		$this->assertEquals($expected, $actual);
	}

	function test_get_filename_extension_failure(){
		
		$actual = ConvertHelper::get_filename_extension('sparql-xml', 'turtle' );
		$expected = false;
		$this->assertEquals($expected, $actual);
	}
	

	function test_get_filename_extension_conneg_json(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-xml', false, array('application/json') );
		$expected = 'json';
		$this->assertEquals($expected, $actual);
	}

	function test_get_filename_extension_conneg_rdfxml(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-xml', false, array('application/rdf+xml') );
		$expected = 'rdf';
		$this->assertEquals($expected, $actual);
	}

	function test_get_filename_extension_conneg_html(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-xml', false, array('text/html') );
		$expected = 'html';
		$this->assertEquals($expected, $actual);
	}

	function test_get_filename_extension_conneg_rdfxml2(){
		
		$actual = ConvertHelper::get_filename_extension('html', false, array('application/rdf+xml') );
		$expected = 'rdf';
		$this->assertEquals($expected, $actual);
	}

	function test_get_filename_extension_conneg_sparqljson(){
		
		$actual = ConvertHelper::get_filename_extension('sparql-xml', false, array('application/sparql-results+json') );
		$expected = 'srj';
		$this->assertEquals($expected, $actual);
	}

	// function test_get_filename_extension_conneg_sparqlxml(){
	// 	
	// 	$actual = ConvertHelper::get_filename_extension('sparql-json', false, array('application/sparql-results+xml') );
	// 	$expected = 'json';
	// 	$this->assertEquals($expected, $actual);
	// }

	
	function test_get_filename_extension_conneg_failure(){
		
		$actual = ConvertHelper::get_filename_extension('sparql-xml', false, array('application/rdf+xml') );
		$expected = false;
		$this->assertEquals($expected, $actual);
	}
	
	
	
	function test_get_filename_extension_exhibit(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-xml', 'exhibit-jsonp' );
		$expected = 'jsonp';
		$this->assertEquals($expected, $actual);
	}
	
	function test_get_filename_extension_html(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-html', 'rdf' );
		$expected = 'rdf';
		$this->assertEquals($expected, $actual);
	}

	function test_get_filename_extension_rss(){
		
		$actual = ConvertHelper::get_filename_extension('rdf-xml', 'rss' );
		$expected = 'rss';
		$this->assertEquals($expected, $actual);
	}


	
	
}

?>